<?php

namespace app\controller;

use support\Http;
use support\Request;
use think\facade\Db;
use Tinywan\ExceptionHandler\Exception\BadRequestHttpException;

class IndexController
{
    public function index(Request $request)
    {
        $a = 'aaa';
        $b = 'bbb';

        return json(apiSuccess([]));
    }

    public function view(Request $request)
    {
        //抛出异常
        throw new BadRequestHttpException('账号或密码不能为空');

        //return view('index/view', ['name' => 'webman']);
    }

    public function json(Request $request)
    {
        $res = (new Http())->get('https://www.baidu.com', [
            'wd' => 'aaa',
        ]);
        return json(apiSuccess([$res]));
    }

    //导入SQL文件
    public function sql(Request $request)
    {
        // 加载 SQL 文件
        $sqlFile = file_get_contents('app/install.sql');

        // 按分号分割多个 SQL 语句（如果 SQL 文件包含多条语句）
        $sqlStatements = array_filter(explode(';', $sqlFile));

        // 执行每条 SQL 语句
        foreach ($sqlStatements as $statement) {
            $trimmedStatement = trim($statement);
            if (!empty($trimmedStatement)) {
                Db::execute($trimmedStatement);
            }
        }
        return json(apiSuccess([], '导入完成'));
    }


}
